package chapter2;

import java.util.Scanner;

public class ReversePart {
    private  ListNode reversePart(ListNode head, int from, int to) {
        int len = 0;
        ListNode fPre = null;
        ListNode tPos = null;
        ListNode current = head;
        while (current != null) {
            len++;
            fPre = len == from - 1 ? current : fPre;
            tPos = len == to + 1 ? current : tPos;
            current = current.next;
        }
        if (from > to || from < 1 || to > len) {
            return head;
        }

        ListNode pre = fPre == null ? head : fPre.next;
        current = pre.next;
        pre.next = tPos;
        ListNode next = null;
        while (current != tPos) {
            next = current.next;
            current.next = pre;
            pre = current;
            current = next;
        }

        if (fPre != null) {
            fPre.next = pre;
            return head;
        }
        return pre;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ListNode dummy = new ListNode(-1);
        ListNode current = dummy;
        String[] strs = sc.nextLine().trim().split(" ");
        for (String str : strs) {
            current.next = new ListNode(Integer.parseInt(str));
            current = current.next;
        }
        ListNode  head = dummy.next;
        int from = sc.nextInt();
        int to = sc.nextInt();
        ListNode res = new ReversePart().reversePart(head, from, to);
        StringBuilder s = new StringBuilder("");
        while(res != null) {
            s.append(res.value).append(" ");
            res = res.next;
        }
        System.out.print(s.toString().trim());
    }
}
